package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.admin.AdminCommonDaoImpl;
import com.yanqu.road.dao.impl.servercenter.ServerInfoDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.mall.ShieldMallMarkIdData;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 72.商品屏蔽
 */
@RestController
@RequestMapping("/usercenter/admin")
public class CenterMallInfoShieldController {

    /**
     * 72.1 查询列表
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/list", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        int goodsId = jsonObject.getIntValue("goodsId");
        String goodsName = jsonObject.getString("goodsName");

        int pageNum = jsonObject.getIntValue("pageNum");
        int pageSize = jsonObject.getIntValue("pageSize");

        List<ShieldMallMarkIdData> list = new ArrayList<>();
        Map<Integer, ShieldMallMarkIdData> dataMap = new ServerInfoDaoImpl().getShieldMallMarkIdDataList(channelId);
        for (ShieldMallMarkIdData data : dataMap.values()) {
            if(goodsId > 0){
                if(data.getMallMarkId() != goodsId){
                    continue;
                }
            }
            if(!StringUtils.isNullOrEmpty(goodsName)){
                if(!data.getMallName().contains(goodsName)){
                    continue;
                }
            }
            list.add(data);
        }
//        int startIndex = (pageNum - 1) * pageSize;
//        startIndex = Math.max(0, startIndex);
//        List<ShieldMallMarkIdData> searchList = new ArrayList<>();
//        for (int i = startIndex; i < startIndex + pageSize && i < list.size(); i++) {
//            searchList.add(list.get(i));
//        }
        AdminResult result = AdminResult.defaultInstance();
        List<Map<String, Object>> mapList = new ArrayList<>(list.size());
        for (ShieldMallMarkIdData data : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("channelId", data.getChannelId());
            map.put("goodsId", data.getMallMarkId());
            map.put("goodsName", data.getMallName());
            map.put("status", data.getState());
            map.put("remark", data.getRemark());
            map.put("goodsType", data.getGoodsType());
            mapList.add(map);
        }
        result.getData().put("rows", mapList);
        result.getData().put("total", mapList.size());
        return result.toJsonString();
    }

    /**
     * 72.2 新增
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/add", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldAdd(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        int goodsId = jsonObject.getIntValue("goodsId");
        int status = jsonObject.getIntValue("status");
        String goodsName = jsonObject.getString("goodsName");
        String remark = jsonObject.getString("remark");

        ShieldMallMarkIdData data = new ShieldMallMarkIdData();
        data.setState(status);
        data.setChannelId(channelId);
        data.setMallMarkId(goodsId);
        data.setMallName(goodsName);
        data.setRemark(remark);
        boolean b = new AdminCommonDaoImpl().batchInsertShieldMallMarkIdData(Collections.singletonList(data));

        if(!b){
            return AdminResult.failMsg("新增失败").toJsonString();
        }

        return AdminResult.defaultInstance().toJsonString();
    }

    /**
     * 72.3 修改
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/update", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldUpdate(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        int goodsId = jsonObject.getIntValue("goodsId");
        int status = jsonObject.getIntValue("status");
        String remark = jsonObject.getString("remark");

        ShieldMallMarkIdData data = new ShieldMallMarkIdData();
        data.setState(status);
        data.setChannelId(channelId);
        data.setMallMarkId(goodsId);
        data.setRemark(remark);

        boolean b = new AdminCommonDaoImpl().batchUpdateShieldMallMarkIdData(Collections.singletonList(data));

        if(!b){
            return AdminResult.failMsg("更新失败").toJsonString();
        }

        return AdminResult.defaultInstance().toJsonString();
    }

    /**
     * 72.4 批量修改屏蔽状态
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/batchUpdate", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldBatchUpdate(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        JSONArray goodsIdList = jsonObject.getJSONArray("goodsIdList");
        int status = jsonObject.getIntValue("status");

        List<ShieldMallMarkIdData> list = new ArrayList<>();
        for (int i = goodsIdList.size() - 1; i >= 0; i--) {
            int id = goodsIdList.getIntValue(i);
            ShieldMallMarkIdData data = new ShieldMallMarkIdData();
            data.setState(status);
            data.setChannelId(channelId);
            data.setMallMarkId(id);
            list.add(data);
        }

        boolean b = new AdminCommonDaoImpl().batchUpdateShieldMallMarkIdData(list);

        if(!b){
            return AdminResult.failMsg("批量修改屏失败").toJsonString();
        }

        return AdminResult.defaultInstance().toJsonString();
    }


    /**
     * 72.5 删除（改为批量删除）
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/del", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldDel(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        JSONArray goodsIds = jsonObject.getJSONArray("goodsIds");
        List<Integer> goodsIdList = new ArrayList<>();
        for (int i = 0; i < goodsIds.size(); i++) {
            goodsIdList.add(goodsIds.getIntValue(i));
        }
        boolean b = new AdminCommonDaoImpl().batchDeleteShieldMallMarkIdData(channelId, goodsIdList);

        if(!b){
            return AdminResult.failMsg("删除失败").toJsonString();
        }

        return AdminResult.defaultInstance().toJsonString();
    }

    /**
     * 72.6 刷新
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/refresh", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldRefresh(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");

        String msg = CenterReloadHelper.serverMgrReload(ServerListMgr.getOpenServerList(channelId), "HideMallMgr", 10);

        AdminResult result = AdminResult.defaultInstance();
        result.setMsg(msg);
        return result.toJsonString();
    }


    /**
     * 72.7 导入
     */
    @ResponseBody
    @RequestMapping(value = "mall/shield/import", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String shieldImport(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        JSONArray goodsIdList = jsonObject.getJSONArray("importData");

        List<ShieldMallMarkIdData> list = new ArrayList<>();
        for (int i = goodsIdList.size() - 1; i >= 0; i--) {
            JSONObject obj = goodsIdList.getJSONObject(i);
            ShieldMallMarkIdData data = new ShieldMallMarkIdData();
            data.setChannelId(channelId);
            data.setMallMarkId(obj.getIntValue("goodsId"));
            data.setMallName(obj.getString("remark"));
            if(obj.containsKey("goodsName")){
                data.setMallName(obj.getString("goodsName"));
            }if(obj.containsKey("goodsType")){
                data.setGoodsType(obj.getIntValue("goodsType"));
            }
            list.add(data);
        }

        boolean b = new AdminCommonDaoImpl().batchInsertShieldMallMarkIdData(list);

        if(!b){
            return AdminResult.failMsg("批量修改屏失败").toJsonString();
        }

        return AdminResult.defaultInstance().toJsonString();
    }
}
